package mhe.mgt.model;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name = "MCHMACHINEPART")
public class MchMachinePart implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7717974337423281742L;
	private long id;
	private Long machineId;
	private String name;
	private Date createDate;
	private Date updateDate;
	private String note;
	private String createUser;
	private int maxLifeDay;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "ID", unique = true, nullable = false, precision = 19, scale = 0)
	public long getId() {
		return id;
	}
	
	public void setId(long id) {
		this.id = id;
	}

	@Column(name = "MACHINE_ID", nullable = false, precision = 19, scale = 0)
	public Long getMachineId() {
		return machineId;
	}

	public void setMachineId(Long machineId) {
		this.machineId = machineId;
	}

	@Column(name = "MAX_LIFE_DAY", nullable = false, precision = 10, scale = 0)
	public int getMaxLifeDay() {
		return maxLifeDay;
	}

	public void setMaxLifeDay(int maxLifeDay) {
		this.maxLifeDay = maxLifeDay;
	}

	@Column(name = "NAME", length = 50, nullable = false)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "CREATE_DATE", nullable = false, length = 7)
	public Date getCreateDate() {
//		Calendar ca = Calendar.getInstance();
//		ca.setTime(createDate);
//		ca.add(Calendar.DAY_OF_MONTH, -1);
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "UPDATE_DATE", nullable = false, length = 7)
	public Date getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}
	
	@Column(name = "NOTE", length = 5000, nullable = true)
	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}
	
	@Column(name = "CREATE_USER", length = 50, nullable = true)
	public String getCreateUser() {
		return createUser;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}
	
	@Transient
	public Date getExpiredDate() {
		Calendar ca = Calendar.getInstance();
		ca.setTime(createDate);
		ca.add(Calendar.DAY_OF_MONTH, maxLifeDay);
		return ca.getTime();
	}

	@Transient
	public int getRemainDay() {
		Calendar cur = Calendar.getInstance();
		long ms = cur.getTimeInMillis() - createDate.getTime();
		int num = (int) (ms / (24 * 60 * 60 * 1000));
		return maxLifeDay - num;
	}
	
	@Transient
	public float getRemainRate() {
		return getRemainDay() * 1.0f / maxLifeDay;
	}
}
